﻿// Author: Radu-Mihail
// Project: ClientInterface, File: Client.cs
// Namespace: Client, Class: Client
// Path: G:\Secure Project\WebCrawlerProject\ClientInterface
// Code lines: 139, Size of file: 4.06 KB
// Creation date: 1/12/2009 3:22 PM
// Last modified: 1/12/2009 8:54 PM

#region Using directives
using Error;
using PageCrawlQueue;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;
#endregion

namespace Client
{
    /// <summary>
    /// Send to thread
    /// </summary>
    

    public partial class Client : Form
    {
        UDPClient.Client udpClient;
        string[] settingsLines;
        bool logged;
        bool fix;
        OurCrawler.ThreadManager threadManager;
        public Client()
        {
            Control.CheckForIllegalCrossThreadCalls = false;
            InitializeComponent();
            udpClient = new UDPClient.Client();
            threadManager = new OurCrawler.ThreadManager();
            settingsLines = File.ReadAllLines("clientSettings.txt");
            logged = false;
            if (settingsLines[3].Equals("fixed"))
                fix = true;
            else
                fix = false;

        }





        private void connect_btn_Click(object sender, EventArgs e)
        {
            string link = null;
            threadManager.threadCount = Convert.ToInt32(settingsLines[2]);
            try
            {
                logged = udpClient.Login(settingsLines[0], settingsLines[1]);
                if (logged)
                {
                    connect_btn.Enabled = false;
                }
            }
            catch (Exception exc)
            {
                Error.ErrorManagement.ManageException(exc);
                return;
            }

            if (logged)
            {
                threadManager.Start(fix);
                for (int i = 0; i < threadManager.threadCount; i++)
                {

                    link = udpClient.AskForSeedLink();

                    if (null != link)
                    {
                        
                        if (threadManager.CanAdd)
                        {
                            //Start(link);
                            SendToThread infoThread = new SendToThread(i, link, ref threadList_lview);
                            threadManager.lThreads.Add(new Thread(new ParameterizedThreadStart(Start)));
                            threadManager.lThreads[i].Start(infoThread);
                        }
                    }
                }
            }
        }

        private void Start(object o)
        {

            SendToThread infoThread = (SendToThread)o;
            
            
            infoThread.view.Items.Add(infoThread.index.ToString());

            while ( null != infoThread.link )
            {
                IQueue bfsQueue = QueueFactory.GetQueue(infoThread.link, 2, -1, QueueType.BFS_QUEUE);
                int depth = 0;

                while (depth < 2)
                {
                    //Monitor.Enter(this);
                    string[] subItems = { "" };
                    if (bfsQueue.LinksToVisit.Count > 0)
                    {
                        infoThread.view.Items[infoThread.index].SubItems.AddRange(subItems);
                        infoThread.view.Items[infoThread.index].SubItems[1].Text = bfsQueue.LinksToVisit[0];
                    } // if (bfsQueue.LinksToVisit.Count)
                    depth = bfsQueue.RunQueue(depth);
                    // Monitor.Exit(this);
                } // block
                infoThread.link = udpClient.AskForSeedLink();
            }

        }

        private void settings_btn_Click(object sender, EventArgs e)
        {
            ClientSettings clientSettings = new ClientSettings();
            clientSettings.Show();
        }

        private void exit_btn_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }



    }

    public class SendToThread
    {
        public int index;
        public string link;
        public ListView view;
        /// <summary>
        /// Create send to thread
        /// </summary>
        /// <param name="i">I</param>
        /// <param name="l">L</param>
        public SendToThread(int i, string l, ref ListView v)
        {
            index = i;
            link = l;
            view = v;
        } // SendToThread(i, l)
    } // struct SendToThread
}
